import {
    OnInit,
    Injector,
    Component,
    Output,
    ViewChild,
    ElementRef,
    EventEmitter,
    ViewEncapsulation,
    Input
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TreeNode, MenuItem } from 'primeng/api';
import { ArrayToTreeConverterService } from '@shared/utils/array-to-tree-converter.service';
import { TreeDataHelperService } from '@shared/utils/tree-data-helper.service';
import * as _ from 'lodash';
import { CodeItemServiceProxy, CodeItemDto } from '@shared/service-proxies/stock-service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { IBasicModelsInfo } from './base-models-info';
export interface IBasicModelsOnTree extends IBasicModelsInfo {
    id: number;
    parent: string | number;
    code: string;
    displayName: string;
    memberCount: number;
    roleCount: number;
    text: string;
    state: any;
    value: string;
}
@Component({
    selector: 'codeitems-tree',
    templateUrl: './codeitems-tree.component.html',
    styleUrls: ['./codeitems-tree.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class CodeItemsTreeComponent extends AppComponentBase implements OnInit {
    @Output() ouSelected = new EventEmitter<IBasicModelsInfo>();
    @Output() ouallSelected = new EventEmitter<IBasicModelsInfo>();
    @Input() stocktype = '';
    _baseModelsService: any;
    createOrEditDataItemModal: any;
    constructor(
        injector: Injector,
        private _dataItemService: CodeItemServiceProxy,
        private _arrayToTreeConverterService: ArrayToTreeConverterService,
        private _treeDataHelperService: TreeDataHelperService
    ) {
        super(injector);
    }

    treeData: any;
    selectedItem: TreeNode;
    // parentId: number; //父级ID
    // systemId: number; //系统名称ID
    // modelId: number; //型号
    // phaseId: number; //阶段
    // itemContextMenuItems: MenuItem[];
    parentId;
    itemDto: CodeItemDto = new CodeItemDto();
    treeheight = { height: '100px' };
    ngOnInit(): void {
        this.getTreeDataFromServer();
        // this.itemContextMenuItems = this.getContextMenuItems();
    }
    //节点展开
    nodeExpand(event) {
        this.getTree(event);
    }
    //节点选中
    nodeSelect(event) {
        this.ouallSelected.emit(<IBasicModelsInfo>{
            displayName: event.node.data.displayName,
            Id: event.node.data.id
        });
    }

    nodeDrop(event) {}

    reload(event): void {
        this.getTreeDataFromServer();
    }
    private getTree(event): void {
        this._dataItemService.getCodeItemTree(event.node.data.id, '').subscribe(result => {
            console.log(result);
            event.node.children = this._arrayToTreeConverterService.createTree(
                result.items,
                'parentId',
                'id',
                null,
                'children',
                [
                    {
                        target: 'label',
                        targetFunction(item) {
                            return item.displayName;
                        }
                    },
                    {
                        target: 'expandedIcon',
                        value: 'fa fa-folder-open m--font-warning'
                    },
                    {
                        target: 'collapsedIcon',
                        value: 'fa fa-folder m--font-warning'
                    },
                    {
                        target: 'selectable',
                        value: true
                    },
                    {
                        target: 'leaf',
                        targetFunction(item) {
                            return item.memberCount<1;
                        }
                    }
                ]
            );
        });
    }
    //获取树
    public getTreeDataFromServer(): void {
        this._dataItemService.getCodeItemTree(this.parentId, this.stocktype).subscribe(result => {
            this.treeData = this._arrayToTreeConverterService.createTree(
                result.items,
                'parentId',
                'id',
                null,
                'children',
                [
                    {
                        target: 'label',
                        targetFunction(item) {
                            return item.displayName;
                        }
                    },
                    {
                        target: 'expandedIcon',
                        value: 'fa fa-folder-open  m--font-warning'
                    },
                    {
                        target: 'collapsedIcon',
                        value: 'fa fa-folder m--font-warning'
                    },
                    {
                        target: 'selectable',
                        value: true
                    },
                    {
                        target: 'leaf',
                        value: false
                    }
                    // {
                    //     target: 'expanded',
                    //     targetFunction(item) {
                    //         if (item.id == -1) {
                    //             return true;
                    //         }
                    //     }
                    // }
                ]
            );
        });
    }

    resetTreeHeight(h: number) {
        let uiTreeEl = document.getElementsByClassName('ui-tree')[0];
        if (uiTreeEl) {
            let height = uiTreeEl.getBoundingClientRect().height;
            console.log(height);
            console.log(h);
            height = Math.max(height, h);

            uiTreeEl.setAttribute('style', 'height:' + height + 'px');
        }
    }
    //右键
    // private getContextMenuItems(): any[] {
    //     let items = [
    //         {
    //             label: this.l('关联PBOM'),
    //             // disabled: !canManageOrganizationTree,
    //             command: event => {
    //                 this.bindPBomModal.show(this.selectedItem.data.code, this.selectedItem.data.id);
    //             }
    //         },
    //         {
    //             label: this.l('查看PBOM'),
    //             // disabled: !nManageOrganizationTree,
    //             command: event => {
    //                 this.bindPBomModal.show(this.selectedItem.data.code, this.selectedItem.data.id);
    //             }
    //         }
    //     ];
    //     return items;
    // }
}
